package com.baofu.paycore.service;

import com.baofu.paycore.common.utils.DateUtil;
import com.baofu.paycore.manager.storage.DispatchManager;
import com.baofu.paycore.service.facade.DispatchServiceFacade;
import com.baofu.paycore.service.facade.model.PaycorePageDTO;
import com.baofu.paycore.service.facade.model.dispatch.DispatchModifyReqDTO;
import com.baofu.paycore.service.facade.model.dispatch.DispatchQueryReqDTO;
import com.baofu.paycore.service.facade.model.dispatch.DispatchResDTO;
import com.system.commons.result.PageReqDTO;
import com.system.commons.result.PageRespDTO;
import com.system.commons.result.Result;
import com.system.commons.utils.BeanCopyUtil;
import com.system.commons.utils.ExceptionUtils;
import com.system.commons.utils.PageUtil;
import com.system.commons.utils.ParamValidate;
import com.system.dispatch.dal.model.BizCmdDO;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.slf4j.helpers.SystemMarker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * dispatch服务
 *
 * <p>
 *  1.更新dispatch信息
 *  2.依ID查询dispatch信息
 *  3.分页查询dispatch信息
 * </p>
 * User: xin deng  Date: 2016/12/6 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Service
public class DispatchServiceImpl implements DispatchServiceFacade {

    /**
     * dispatch
     */
    @Autowired
    private DispatchManager dispatchManager;

    /**
     * 更新dispatch信息
     *
     * @param bean       修改参数
     * @param traceLogId 日志ID
     * @return 更新成功
     */
    @Override
    public Result<Boolean> updateDispatch(DispatchModifyReqDTO bean, String traceLogId) {

        MDC.put(SystemMarker.TRACE_LOG_ID, traceLogId);
        log.info("更新dispatch信息传入对象：{}", bean);
        Result<Boolean> result;
        try {
            //校验对象
            ParamValidate.validateParams(bean);
            //转化对象
            BizCmdDO channelInfoDO = BeanCopyUtil.objConvert(bean, BizCmdDO.class);
            channelInfoDO.setUpdatedBy(bean.getOperator());
            dispatchManager.updateDispatch(channelInfoDO);
            result = new Result<>(true);

        } catch (Exception e) {
            log.error("更新dispatch信息异常：{}", e);
            result = ExceptionUtils.getResponse(e);
        }
        log.info("更新dispatch信息返回对象：{}", result);
        return result;
    }

    /**
     * 依ID查询dispatch信息
     *
     * @param id         ID
     * @param traceLogId 日志ID
     * @return dispatch信息
     */
    @Override
    public Result<DispatchResDTO> queryDispatchById(String id, String traceLogId) {

        MDC.put(SystemMarker.TRACE_LOG_ID, traceLogId);
        log.info("依ID查询dispatch信息,ID：{}", id);
        Result<DispatchResDTO> result;
        try {
            //校验对象
            ParamValidate.validateNull(id);

            BizCmdDO bizCmdDO = dispatchManager.getDispatchById(id);
            result = new Result<>(BeanCopyUtil.objConvert(bizCmdDO, DispatchResDTO.class));

        } catch (Exception e) {
            log.error("更新dispatch信息异常：{}", e);
            result = ExceptionUtils.getResponse(e);
        }
        log.info("更新dispatch信息返回对象：{}", result);
        return result;
    }

    /**
     * 分页查询dispatch信息
     *
     * @param bean       查询参数
     * @param pageReqDTO 分页参数
     * @param traceLogId 日志ID
     * @return dispatch信息
     */
    @Override
    public Result<PageRespDTO<DispatchResDTO>> queryDispatchPage(DispatchQueryReqDTO bean, PageReqDTO pageReqDTO,
                                                                 String traceLogId) {
        MDC.put(SystemMarker.TRACE_LOG_ID, traceLogId);
        log.info("分页查询dispatch信息传入参数：{},{}", bean, pageReqDTO);
        Result<PageRespDTO<DispatchResDTO>> result;
        try {
            //校验分页对象
            PageUtil.checkedPageParam(pageReqDTO);
            //校验对象
            ParamValidate.validateParams(bean);
            //转化对象
            BizCmdDO bizCmdDO = BeanCopyUtil.objConvert(bean, BizCmdDO.class);
            bizCmdDO.setStartDate(bean.getStartDate());
            bizCmdDO.setEndDate(DateUtil.addDays(bean.getEndDate(),24*60*60));
            int count = dispatchManager.getDispatchCount(bizCmdDO);
            List<BizCmdDO> bizCmdDOList = dispatchManager.getDispatchPage(
                    bizCmdDO, pageReqDTO.getStartRow(), pageReqDTO.getPageCount());
            //转化对象
            List<DispatchResDTO> dispatchResDTOs =
                    BeanCopyUtil.convertList(bizCmdDOList, DispatchResDTO.class);
            result = new Result<>(
                    new PageRespDTO<>(count, pageReqDTO.getPageCount(), dispatchResDTOs));

        } catch (Exception e) {
            log.error("分页查询dispatch信息异常：{}", e);
            result = ExceptionUtils.getResponse(e);
        }

        log.info("分页查询dispatch信息返回结果：{}", result);
        return result;
    }
}
